<?php

namespace app\manystore\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Student extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'manystore_student';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'gender_text',
        'parent_student'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getGenderList()
    {
        return ['0' => __('Gender 0'), '1' => __('Gender 1')];
    }

    public function getParentStudentAttr($value, $data)
    {
        $list = Db::name('manystore_parent_student')->where(['student_id' => $data['id']])->field('id,parent_id,relation,contact')->select();
        return json_encode($list);
    }

    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
